package com.zdb.java.lambda;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;

public class TestLam {

    public static void main(String[] args) {
        String pathName = "/tmp/ILam.obj";
//        save(pathName);
//        ILam lam = load(pathName);


        ILam lam = (String obj) -> {
            System.out.println("----- ILam 1----- " + obj);
        };
        lam.test("---11---");
        try {

            //            String pathName = "/tmp/ILam.obj";
            File f = new File(pathName);
            if(f.exists()) {
                f.delete();
            }

            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(pathName));
            oos.writeObject(lam);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(pathName));
            Object obj = ois.readObject();
            ois.close();
            ((ILam) obj).test("--2--");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
    private static void save(String pathName) {
        ILam lam = (TestObj obj) -> {
            System.out.println("----- ILam 1----- " + obj.getI());
        };
        try {

//            String pathName = "/tmp/ILam.obj";
            File f = new File(pathName);
            if(f.exists()) {
                f.delete();
            }

            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(pathName));
            oos.writeObject(lam);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static ILam load(String pathName) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(pathName));
            Object obj = ois.readObject();
            ois.close();
            return (ILam) obj;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    */
    
//    public static Object $deserializeLambda$(SerializedLambda serializedLambda) {
//        System.out.println("---- custom $deserializeLambda$ -----");
//        return new ILam() {
//            @Override public void test(TestObj obj) {
//                System.out.println("----$deserializeLambda$----" + obj.getI());
//            }
//        };
//    }

}

@FunctionalInterface
interface ILam extends Serializable {

    void test(String obj);

}


